// pages/editPrescription/editPrescription.js
import Toast from '@vant/weapp/toast/toast';
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    advice: '',
    showPopup: false,
    dateStr: '',
    date: new Date().getTime(),
    minDate: new Date(2020, 0, 1).getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}`;
      }
      if (type === 'month') {
        return `${value}`;
      }
      return `${value}`;
    },
    recent: 0,
    activeName: "drugs",
    prescription: {
      name: "",
      advice: "",
      date: "",
      recent: 0,
      drugs: []
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const date = new Date();
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    this.setData({
      dateStr: `${year}-${month}-${day}`
    });
    this.confirmDate();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(app.globalData.selectDrugs)
    if(app.globalData.selectDrugs!=null){
      this.setData({
        'prescription.drugs':app.globalData.selectDrugs
      })
    }
    app.globalData.selectDrugs = []
  },

  onChangeName() {
    console.log(this.data.name)
    let prescription = this.data.prescription
    prescription.name = this.data.name
    this.setData({
      prescription: prescription,
    })
    console.log(prescription)
  },

  onChangeAdvice() {
    console.log(this.data.advice)
    let prescription = this.data.prescription
    prescription.advice = this.data.advice
    this.setData({
      prescription: prescription,
    })
    console.log(prescription)
  },

  onSelectDate(event) {
    console.log(event)
    const date = new Date(event.detail);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    this.setData({
      date: event.detail,
      dateStr: `${year}-${month}-${day}`
    });
  },

  confirmDate() {
    let prescription = this.data.prescription
    prescription.date = this.data.dateStr
    this.setData({
      prescription: prescription,
      showPopup: false
    })
    console.log(prescription)
  },

  showDatePicker() {
    this.setData({
      showPopup: true,
    });
  },

  closeDatePicker() {
    this.setData({
      showPopup: false,
    });
  },

  onChangeRecent({
    detail
  }) {
    this.setData({
      recent: detail ? 1 : 0
    });
    let prescription = this.data.prescription
    prescription.recent = this.data.recent
    this.setData({
      prescription: prescription
    })
    console.log(prescription)
  },

  onChange(event) {
    this.setData({
      activeName: event.detail,
      openPid: event.detail
    });
  },

  selectDrugs() {
    wx.navigateTo({
      url: '/pages/selectDrugs/selectDrugs?from=editPrescription',
    })
  },

  removeDrug(event) {
    console.log(event)
    const did = event.currentTarget.id
    var drugs = this.data.prescription.drugs.filter(drug => {
      return drug.id != did;
    })
    this.setData({
      'prescription.drugs': drugs
    })
    console.log(this.data.prescription)
  },

  async onSubmit() {
    if (this.data.prescription.name != '') {
      // 新建医嘱
      const res = await app.call({
        path: '/prescription/addPrescription',
        method: 'POST',
        data: this.data.prescription
      })
      console.log(res)
      if (res.code === 0) {
        Toast.success("添加成功");
        wx.navigateBack({
          delta: 1
        })
      } else {
        Toast.fail(res.errorMsg);
      }
    } else {
      Toast.success("请填写必填项！");
    }

  }
})